import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**运营邮件的奖励属性枚举*/
export const enum E_AdminMailDrop {
	/**奖励类型,掉落类型,2:道具,3:装备,6:宠物*/
	DROP_TYPE = 0,
	/**道具id*/
	ITEM_ID = 1,
	/**数量*/
	NUM = 2,
	/**装备等级,非装备是0*/
	LEVEL = 3,
	/**枚举数量*/
	EnumCount = 4
}


/**运营邮件的奖励*/
export class AdminMailDrop {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_ADMIN_MAIL_DROP_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_ADMIN_MAIL_DROP_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: AdminMailDrop[] = [];

	/**创建入口*/
	public static create(json?: string): AdminMailDrop {
		const c = this.POOL.pop() || new AdminMailDrop();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**奖励类型,掉落类型,2:道具,3:装备,6:宠物 (uint32)*/
	protected _dropType: number;
	/**道具id (uint32)*/
	protected _itemId: number;
	/**数量 (uint32)*/
	protected _num: number;
	/**装备等级,非装备是0 (uint32)*/
	protected _level: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (AdminMailDrop.POOL.indexOf(this) === -1) {
			AdminMailDrop.POOL.push(this);
		}
	}

	/**奖励类型,掉落类型,2:道具,3:装备,6:宠物*/
	public get dropType() {
		return this._dropType;
	}


	/**奖励类型,掉落类型,2:道具,3:装备,6:宠物*/
	public set dropType(v: number) {
		this._dropType = v;
	}


	/**道具id*/
	public get itemId() {
		return this._itemId;
	}


	/**道具id*/
	public set itemId(v: number) {
		this._itemId = v;
	}


	/**数量*/
	public get num() {
		return this._num;
	}


	/**数量*/
	public set num(v: number) {
		this._num = v;
	}


	/**装备等级,非装备是0*/
	public get level() {
		return this._level;
	}


	/**装备等级,非装备是0*/
	public set level(v: number) {
		this._level = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_AdminMailDrop): any {
		switch (k) {
			case E_AdminMailDrop.DROP_TYPE: return this._dropType;
			case E_AdminMailDrop.ITEM_ID: return this._itemId;
			case E_AdminMailDrop.NUM: return this._num;
			case E_AdminMailDrop.LEVEL: return this._level;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_AdminMailDrop, v: any): void {
		switch (k) {
			case E_AdminMailDrop.DROP_TYPE: this._dropType = v; break;
			case E_AdminMailDrop.ITEM_ID: this._itemId = v; break;
			case E_AdminMailDrop.NUM: this._num = v; break;
			case E_AdminMailDrop.LEVEL: this._level = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//奖励类型,掉落类型,2:道具,3:装备,6:宠物
		this._dropType = 0;
		//道具id
		this._itemId = 0;
		//数量
		this._num = 0;
		//装备等级,非装备是0
		this._level = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._dropType) {
			json.dropType = this._dropType;
		}
		if (this._itemId) {
			json.itemId = this._itemId;
		}
		if (this._num) {
			json.num = this._num;
		}
		if (this._level) {
			json.level = this._level;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.dropType) {
			this._dropType = json.dropType;
		}
		if (json.itemId) {
			this._itemId = json.itemId;
		}
		if (json.num) {
			this._num = json.num;
		}
		if (json.level) {
			this._level = json.level;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._dropType);
		byte.writeUint32(this._itemId);
		byte.writeUint32(this._num);
		byte.writeUint32(this._level);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._dropType = byte.readUint32();
		this._itemId = byte.readUint32();
		this._num = byte.readUint32();
		this._level = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): AdminMailDrop {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = AdminMailDrop.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = AdminMailDrop.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//奖励类型,掉落类型,2:道具,3:装备,6:宠物
			if (idx === E_AdminMailDrop.DROP_TYPE) {
				const oldv = this._dropType;
				this._dropType = us[i + 1];
				IAsync.dispatch(eventID, idx, this._dropType, oldv);
				i += 2;
				continue;
			}
			//道具id
			if (idx === E_AdminMailDrop.ITEM_ID) {
				const oldv = this._itemId;
				this._itemId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._itemId, oldv);
				i += 2;
				continue;
			}
			//数量
			if (idx === E_AdminMailDrop.NUM) {
				const oldv = this._num;
				this._num = us[i + 1];
				IAsync.dispatch(eventID, idx, this._num, oldv);
				i += 2;
				continue;
			}
			//装备等级,非装备是0
			if (idx === E_AdminMailDrop.LEVEL) {
				const oldv = this._level;
				this._level = us[i + 1];
				IAsync.dispatch(eventID, idx, this._level, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in AdminMailDrop with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: AdminMailDrop): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._dropType !== other._dropType) {
			updates.push(E_AdminMailDrop.DROP_TYPE, this._dropType);
		}
		if (this._itemId !== other._itemId) {
			updates.push(E_AdminMailDrop.ITEM_ID, this._itemId);
		}
		if (this._num !== other._num) {
			updates.push(E_AdminMailDrop.NUM, this._num);
		}
		if (this._level !== other._level) {
			updates.push(E_AdminMailDrop.LEVEL, this._level);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(AdminMailDrop.TOTAL_EVENT_ID, this.getV(0));
	}

}
